package com.fxc.impl;

import com.fxc.api.MemberService;
import com.fxc.entity.PO.MemberPO;
import com.fxc.entity.PO.MemberPOExample;
import com.fxc.mapper.MemberPOMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author FXC
 * @version 1.0
 * @date 2021/12/3 0:25
 */
// 开始事务
@Transactional(readOnly = true)
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberPOMapper memberPOMapper;

    @Override
    public MemberPO getMemberService(String loginacct) {

        // 1.创建Example对象
        MemberPOExample example = new MemberPOExample();

        // 2，创建Criteria对象
        MemberPOExample.Criteria criteria = example.createCriteria();

        // 3.封装查询条件
        criteria.andLoginacctEqualTo(loginacct);

        // 4执行查询
        List<MemberPO> list = memberPOMapper.selectByExample(example);

        // 5.获取结果
        if (list == null || list.size() == 0){
            return null;
        }
        return list.get(0);
    }

    @Transactional(
            propagation = Propagation.REQUIRES_NEW,
            rollbackFor = Exception.class,
            readOnly = false)
    @Override
    public void saveMember(MemberPO memberPO) {

        memberPOMapper.insertSelective(memberPO);

    }
}
